﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Psm.Logging;
using Psm.Container;
using System.Configuration;

namespace Psm.Transport.TcpTransport
{
    class TcpHostsConfiguration : ConfigurationSection
    {
        private static ILogger logger = Repository.GetInstance<ILogger>();
        private static TcpHostsConfiguration configuration;

        public static TcpHostsConfiguration GetConfiguration()
        {
            if (configuration == null)
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configuration = (TcpHostsConfiguration)config.GetSection("TcpHosts");
            }
            return configuration;
        }

        public static TcpHostConfiguration GetConfiguration(string name)
        {
            foreach (TcpHostConfiguration config in GetConfiguration().TcpHosts)
            {
                if (config.Name.Equals(name)) return config;
            }
            return null;
        }

        [ConfigurationProperty("", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
        public TcpHostConfigurationCollection TcpHosts
        {
            get
            {
                return base[""] as TcpHostConfigurationCollection;
            }

            set
            {
                base[""] = value;
            }
        }
    }

    public class TcpHostConfigurationCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new TcpHostConfiguration();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TcpHostConfiguration)element).Port;
        }

        protected override string ElementName
        {
            get
            {
                return "TcpHost";
            }
        }

        protected override bool IsElementName(string elementName)
        {
            return !String.IsNullOrEmpty(elementName) && elementName == "TcpHost";
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        public TcpHostConfiguration this[int index]
        {
            get
            {
                return base.BaseGet(index) as TcpHostConfiguration;
            }
        }

    }

    public class TcpHostConfiguration : ConfigurationElement
    {
        public TcpHostConfiguration()
        {
        }

        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return base["name"] as string; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("ip", IsRequired = true, IsKey = true)]
        public string Ip
        {
            get { return base["ip"] as string; }
            set { base["ip"] = value; }
        }

        [ConfigurationProperty("port", IsRequired = true, IsKey = true)]
        public string Port
        {
            get { return base["port"] as string; }
            set { base["port"] = value; }
        }
    }
}
